using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using ServiceMReviewer.Model;
using ServiceMReviewer.Helper;

namespace ServiceMReviewer.DTO
{
	/// <summary>
	/// Data transfer object which is associated with Establishment table in database
	/// </summary>
	[DataContract]
	public class EstablishmentDTO
	{
		[DataMember]
		public string ID { get; set; }
        [DataMember]
        public int IDEstablishmentType { get; set; }
        [DataMember]
        public int IDFoodType { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Location { get; set; }

        /// <summary>
        /// The average rating 
        /// </summary>
        [DataMember]
        public double AverageRating { get; set; }

        /// <summary>
        /// The number of reviewer
        /// </summary>
        [DataMember]
        public int NumReviewer { get; set; }

		[DataMember]
        public EstablishmentTypeDTO EstablishmentType { get; set; }
		[DataMember]
        public FoodTypeDTO FoodType { get; set; }
        [DataMember]
        public List<ReviewDTO> Reviews { get; set; }

		public static EstablishmentDTO Translate(Establishment Entity)
		{
            if (Entity != null)
            {
                var dto = new EstablishmentDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    Location = Entity.Location,
                };
                if (Entity.EstablishmentType != null)
                {
                    dto.IDEstablishmentType = Entity.IDEstablishmentType.Value;
                    dto.EstablishmentType = EstablishmentTypeDTO.Translate(Entity.EstablishmentType);
                }
                if (Entity.FoodType != null)
                {
                    dto.IDFoodType = Entity.IDFoodType.Value;
                    dto.FoodType = FoodTypeDTO.Translate(Entity.FoodType);
                }
                if (Entity.Reviews != null)
                {
                    dto.Reviews = new List<ReviewDTO>();
                    foreach (Review item in Entity.Reviews)
                    {
                        dto.Reviews.Add(ReviewDTO.Translate(item));
                    }
                }

                // Build Average Rating
                if (dto.Reviews != null)
                {
                    double totalRating = 0;
                    foreach (ReviewDTO item in dto.Reviews)
                        totalRating += item.OverallRating;
                    if (totalRating > 0)
                        dto.AverageRating = totalRating / dto.Reviews.Count;

                    dto.NumReviewer = dto.Reviews.Count;
                }

                return dto;
            }
			return null;
		}

        public static EstablishmentDTO TranslateBase(Establishment Entity)
		{
            if (Entity != null)
            {
                var dto = new EstablishmentDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    Location = Entity.Location,
                };

                if (Entity.EstablishmentType != null)
                {
                    dto.IDEstablishmentType = Entity.IDEstablishmentType.Value;
                    dto.EstablishmentType = EstablishmentTypeDTO.Translate(Entity.EstablishmentType);
                }

                if (Entity.FoodType != null)
                {
                    dto.IDFoodType = Entity.IDFoodType.Value;
                    dto.FoodType = FoodTypeDTO.Translate(Entity.FoodType);
                }

                return dto;
            }
			return null;
		}

        public static Establishment Reverse(EstablishmentDTO obj)
        {
            var entity = new Establishment
            {
                ID = obj.ID,
                Name = obj.Name,
                Location = obj.Location,
                IDEstablishmentType = obj.IDEstablishmentType,
                IDFoodType = obj.IDFoodType
            };
            return entity;
        }

        public void UpdateEntity(ref Establishment entity)
        {
            entity.Name = this.Name;
            entity.Location = this.Location;
            entity.IDEstablishmentType = this.IDEstablishmentType;
            entity.IDFoodType = this.IDFoodType;
        }
	}
}